package com.zyx.javademo.stream;

import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;
import sun.nio.ch.IOUtil;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * Description:
 * <br/>Stream中常用的方法
 * <br/>reference: <a href="https://houbb.github.io/2019/02/27/java8-14-stream-usage-03-number-stream">Stream 数值流 构建流</a>
 *
 * @author Yaxi.Zhang
 * @since 2022/9/25 23:09
 */
public class StreamDemo {
    public static void main(String[] args) throws URISyntaxException, IOException {
        // iterate
        // 两个参数, 第一个为初始值, 第二个为迭代表达式
        System.out.println(Stream.iterate(0, n -> n + 1).limit(10).collect(Collectors.toList()));

        System.out.println("**************************************************");

        // generate
        Random r = new Random();
        Stream.generate(() -> r.nextInt(10)).limit(10).forEach(System.out::println);
        System.out.println("**************************************************");

        // map
        Stream.iterate(0, n -> n + 1).limit(10).map(x -> x * x).forEach(System.out::println);
        System.out.println("**************************************************");

        // filter
        Stream.iterate(0, n -> n + 1).limit(10).filter(x -> x % 5 == 1).forEach(System.out::println);
        System.out.println("**************************************************");

        // findFirst() 配合 Optional使用
        System.out.println(Stream.iterate(0, n -> n + 1).limit(10).filter(x -> x % 5 == 1).findFirst().orElse(300));
        System.out.println(Stream.iterate(0, n -> n + 1).limit(10).filter(x -> x > 100).findFirst().orElse(300));
        System.out.println("**************************************************");

        // anyMatch: 有一个匹配则返回true, 否则返回false
        System.out.println(Stream.iterate(0, n -> n + 1).limit(10).anyMatch(x -> x % 2 == 1));
        System.out.println(Stream.iterate(0, n -> n + 2).limit(10).anyMatch(x -> x % 2 == 1));
        System.out.println("**************************************************");

        // anyMatch: 所有都匹配则返回true, 否则返回false
        System.out.println(Stream.iterate(0, n -> n + 1).limit(10).allMatch(x -> x % 2 == 1));
        System.out.println(Stream.iterate(0, n -> n + 2).limit(10).allMatch(x -> x % 2 == 0));
        System.out.println("**************************************************");

        // 从1到100的偶数的个数
        System.out.println(IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0).count());

        long uniqueWords;
        URI uri = new ClassPathResource("wc.txt").getURI();
        try (Stream<String> lines = Files.lines(Paths.get(uri), StandardCharsets.UTF_8)) {
            uniqueWords = lines.flatMap(line -> Arrays.stream(line.split(" ")))
                    .distinct()
                    .count();
            System.out.println("uniqueWords:" + uniqueWords);
        } catch (IOException e) {
            e.fillInStackTrace();
        }
    }
}
